package com.rui1024.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rui1024.dao.RegisterDao;
import com.rui1024.dao.impl.RegisterDaoImpl;
import com.rui1024.pojo.User;
import com.rui1024.util.JDBCUtil;


/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		RegisterDaoImpl registerUserdao = new RegisterDaoImpl();
		
		String add_rid = request.getParameter("add_rid");
		String add_rpassword = request.getParameter("add_rpassword");
		String add_rthis = request.getParameter("add_rthis");
		String add_rrole = request.getParameter("add_rrole");

		JDBCUtil jdbcUtil = new JDBCUtil();
		Connection con=null;
		User user = new User();


		try {
			if(add_rid!=null){

				con = jdbcUtil.getCon();
				user.setUserName(add_rid);
				user.setUserPasswd(add_rpassword);
				user.setThis(add_rthis);
				user.setRole(add_rrole);
				registerUserdao.insert(user,con);
				add_rid="";
				add_rpassword="";
				add_rthis="";
				add_rrole="";
			}

			

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				jdbcUtil.closeCon(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		request.getRequestDispatcher("welcome.jsp").forward(request, response);	
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
